package com.ruoyi.project.wenyanwen.generate.convert;

import com.ruoyi.common.utils.ResourcesHelper;
import com.ruoyi.project.dict.zyzx.domain.Zyzx;
import com.ruoyi.project.wenyanwen.generate.domain.sqlite.ZyzxSqlite;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@Component
public class ZyzxConvert extends AbstractConvert {

    public final static Charset CHARSET = Charset.forName("UTF-8");

    protected static Log logger = LogFactory.getLog(ZyzxConvert.class);

    private static byte[] compressSvg(byte[] bytes) {
        String s = new String(bytes, CHARSET);
        s = s.replaceAll("<svg enable-background=\"new 0 0 400 400\" viewBox=\"0 0 400 400\" xmlns=\"http://www.w3.org/2000/svg\">", "<svg xmlns=\"http://www.w3.org/2000/svg\">");
        s = s.replaceFirst("<path.*>(<g fill)", "$1");
        return s.getBytes(CHARSET);
    }

    @Override
    public List<ZyzxSqlite> convert(List<?> list) {

        List<ZyzxSqlite> l = new ArrayList<>();

        for (Object obj : list) {
            Zyzx zyzx = (Zyzx) obj;
            ZyzxSqlite model = new ZyzxSqlite();
            model.setId(zyzx.getId().intValue());
            model.setZiId(zyzx.getZiId());
            model.setType(zyzx.getType());
            model.setMark(zyzx.getMark());
            try {
                byte[] bytes = ResourcesHelper.read("http://img.kkc.cdn.youzhi.net/" + zyzx.getImg(), ".svg");
                model.setImage(zipAndencrypt(compressSvg(bytes)));
            } catch (IOException ie) {
                throw new RuntimeException(ie.getMessage());
            }
            l.add(model);
        }

        return l;
    }

    public static void main(String[] args) throws Exception {
        String url = "http://img.kkc.cdn.youzhi.net/zyzx/bd523417e8ad53922fe9ab6faeecaa3b.svg";
        byte[] bytes = ResourcesHelper.read(url, ".svg");
        byte[] nbytes = compressSvg(bytes);
//        System.out.println(bytes.length);
//        System.out.println(nbytes.length);
        File file = new File("/Users/jackyshi/Downloads/test.png");
        FileOutputStream outputStream = new FileOutputStream(file);
        try {
            PNGTranscoder t = new PNGTranscoder();
            TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
            TranscoderOutput output = new TranscoderOutput(outputStream);
            t.transcode(input, output);
            outputStream.flush();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
